package com.glink.common.util;

import com.glink.common.contants.BaseCodeConstants;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * 静态code抓取器
 * @Author : qiushaoshan
 * @create 2024/6/28 9:04
 */
public class StaticConstantsFetcherUtil {

    /**
     * 抓取类中的常量名
     * @param clazz 类
     * @return 常量名列表
     */
    public static List<String> executeKey(Class<?> clazz){
        List<String> keyList = Lists.newArrayList();
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
                String name = field.getName();
                if(StringUtils.isNotEmpty(name)){
                    name = name.replace("KEY_", "");
                }
                keyList.add(name);
            }
        }
        return keyList;
    }

    /**
     * 抓取类中的常量值
     * @param clazz 类
     * @return 常量名列表
     */
    public static List<String> executeValue(Class<?> clazz){
        List<String> keyList = Lists.newArrayList();
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
                try {
                    String value = (String) field.get(null);
                    keyList.add(value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return keyList;
    }

    /**
     * 根据类别获取下级的键值对
     * @param typeList 类型列表
     * @return 键值对
     */
    public static Map<String, Map<String, String>> executeKeyAndValueBatch(List<String> typeList){
        Map<String, Map<String, String>> keyMap = Maps.newHashMap();
        if(CollectionUtils.isEmpty(typeList)){
            return keyMap;
        }

        String parentName = BaseCodeConstants.class.getName();

        typeList.forEach(type -> {
            Map<String, String> mapMap = Maps.newHashMap();
            String className = parentName + "$CodeType_" + type;
            try {
                Class<?> myClass = Class.forName(className);
                Field[] fields = myClass.getFields();
                for (Field field : fields) {
                    String name = field.getName();
                    String value = (String) field.get(null);
                    mapMap.put(name, value);
                }
                keyMap.put(type, mapMap);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return keyMap;
    }

    public static Map<String, String> executeKeyAndValue(String typeStr){
        Map<String, String> keyMap = Maps.newHashMap();
        if(StringUtils.isEmpty(typeStr)){
            return keyMap;
        }

        String parentName = BaseCodeConstants.class.getName();

        String className = parentName + "$CodeType_" + typeStr;
        try {
            Class<?> myClass = Class.forName(className);
            Field[] fields = myClass.getFields();
            for (Field field : fields) {
                String name = field.getName().replace("KEY_", "");
                String value = (String) field.get(null);
                keyMap.put(name, value);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return keyMap;
    }
}
